from django.http import JsonResponse


class FormatResponse:
    SUCCESS = 0, '成功'  # status
    PARAM_ERROR = 4001, '参数错误'
    ACCESS_RESTRICTION = 4002, '访问限制'
    ACCOUNT_REGISTRATION = 4003, '账户已注册'
    CODE_EXPIRED = 4004, '验证码过期'
    UPLOAD_EXCEPTION = 4005, '上传异常'
    UNKNOWN_ERROR = 4100, '未知错误'

    def __init__(self,status='SUCCESS',data=''):
        '''

        :param status: 传入的状态
        :param data: 传入的参数数据
        '''
        if hasattr(self,status):
            self.code,self.msg = getattr(self,status)
        else:
            status = 'UNKNOWN_ERROR'
            self.code, self.msg = getattr(self, status)
        self.data = data

    def dic(self):
        dic = {'code':self.code}
        if self.msg:
            dic['msg'] = self.msg
        if self.data:
            dic['data'] = self.data
        return dic

    def res(self):
        '''

        :return: 最后构造的返回数据格式为{code:'',msg:'',data:''}
        '''
        return JsonResponse(self.dic(),json_dumps_params={'ensure_ascii':False})








